package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmployeeQuery;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

        @RequiresPermissions(value = {"employee:list", "员工查询"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")EmployeeQuery qo){
        PageInfo pageInfo = employeeService.query(qo);
        List<Department> depts = departmentService.selectAll();
        model.addAttribute("result",pageInfo);
        model.addAttribute("depts",depts);
        return "/employee/list";
    }

    @RequiresPermissions(value = {"employee:input", "员工编辑页面"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("depts",departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);
        //角色回显

        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/input";
    }

    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工新增/编辑"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId() != null) {
            employeeService.updateByPrimaryKey(employee,ids);
        }else {
            employeeService.save(employee,ids);
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"employee:delete", "员工删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        employeeService.deleteByPrimaryKey(id);
        return new JsonResult();
    }

    //批量删除
    @RequiresPermissions(value = {"employee:deleteBatch", "员工批量删除"},logical = Logical.OR)
    @RequestMapping("/deleteBatch")
    @ResponseBody
    public JsonResult deleteBatch(Long[] ids){
        employeeService.deleteBatch(ids);
        return new JsonResult();
    }

    //远程校验
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name){
        Employee employee = employeeService.checkName(name);
        HashMap<String,Boolean> map = new HashMap<>();
        map.put("valid",employee==null);
        return map;
    }

    //导出
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse resp) throws IOException {
        //设置文件下载响应头
        resp.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建Excel文件
        Workbook wb = employeeService.exportXls();
        //把Excel数据输出给浏览器
        wb.write(resp.getOutputStream());

      /*//使用工具进行导出
        List<Employee> employees = employeeService.selectAll();
        EasyExcel.write(resp.getOutputStream(),Employee.class)
                .sheet("员工名单").doWrite(employees);*/
    }

    //导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }
}
